/**
 * @filename:WarehouseServiceImpl Jan 14, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.domain.SysAdmin;
import com.wonder.admin.service.IVtuzxKingdeeService;
import com.wonder.admin.service.SysAdminService;
import com.wonder.app.domain.Location;
import com.wonder.app.domain.Warehouse;
import com.wonder.app.mapper.LocationDao;
import com.wonder.app.mapper.WarehouseDao;
import com.wonder.app.service.WarehouseService;
import com.wonder.app.request.WarehouseRequest;
import com.wonder.bean.kingdee.VtuzxKingdeeCloudClient;
import com.wonder.bean.kingdee.xqc.VtuzxArrayList;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.validation.annotation.Validated;

/**   
 * @Description:TODO(仓库列表服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class WarehouseServiceImpl  extends ServiceImpl<WarehouseDao, Warehouse> implements WarehouseService  {

	@Autowired
	private WarehouseDao warehouseDao;

	@Autowired
	private IVtuzxKingdeeService vtuzxKingdeeService;

	@Autowired
	private LocationDao locationDao;

	@Autowired
	private SysAdminService adminService;

    /**
     * 验证仓库列表信息
     * 
     * @param warehouse
     * @param type
     * @return String
     */
    @Override
    public String validate(Warehouse warehouse,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:仓库列表分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Warehouse>   //分页记录
	 */
	@Override 
	public IPage<Warehouse> selectList(WarehouseRequest request, Page<Warehouse> page){
	    LambdaQueryWrapper<Warehouse> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (null != request.getId()) {
			lambdaQueryWrapper.eq(Warehouse::getId, request.getId());
		}

		if (StringUtils.hasText(request.getStockNumber())) {
			    lambdaQueryWrapper.like(Warehouse::getStockNumber, request.getStockNumber()); 
		    } 
             
        if (StringUtils.hasText(request.getStockName())) {
			    lambdaQueryWrapper.like(Warehouse::getStockName, request.getStockName()); 
		    }

		lambdaQueryWrapper.orderByDesc(Warehouse::getId);
		Page<Warehouse> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Override
	public Boolean checkStock(Map<String, Object> map) throws Exception{
		String stockNo = VtuzxUtil.getString(map,"stockNo");
		VtuzxKingdeeCloudClient client = vtuzxKingdeeService.loginKingdee();
		Boolean FIsOpenLocation = false;
		//查询物料是够启用批号
		List<VtuzxMap> materialQuery = vtuzxKingdeeService.query(client, "BD_STOCK", new VtuzxArrayList<>(
						"FIsOpenLocation"//是否启用仓位
				),
				String.format("FNumber = '%s'", stockNo), null, 0, 1);
		if (!VtuzxUtil.isEmpty(materialQuery)) {
			FIsOpenLocation = VtuzxUtil.getBoolean(materialQuery.get(0),"FIsOpenLocation");
		}
		return FIsOpenLocation;
	}

	@Override
	public List<String> getLocation(Map<String, Object> map) throws Exception {
		String stockNo = VtuzxUtil.getString(map,"stockNo");
		VtuzxKingdeeCloudClient client = vtuzxKingdeeService.loginKingdee();
		List<String> dataList = new ArrayList<>();
		//查询物料是够启用批号
		List<VtuzxMap> materialQuery = vtuzxKingdeeService.query(client, "BD_STOCK", new VtuzxArrayList<>(
						"FFlexEntryId.fnumber",//仓位编码
						"FFlexEntryName"//仓位名称
				),
				String.format("FNumber = '%s'", stockNo), null, 0, 0);
		if (!VtuzxUtil.isEmpty(materialQuery)) {
			for (Map<String,Object> data : materialQuery){
				String locationNo = VtuzxUtil.getString(data,"FFlexEntryId.fnumber");
				dataList.add(locationNo);
			}

		}
		return dataList;
	}


	/**
	 * 查询金蝶所有仓库
	 * @param param
	 * @param user_id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getStockList(Map<String, Object> param, Long user_id) throws Exception {
		VtuzxKingdeeCloudClient client = null;
		try {
			client = vtuzxKingdeeService.loginKingdeeByUser(user_id);
		} catch (Exception e){
			throw new RuntimeException("金蝶账号密码错误，请维护");
		}
		List<Map<String,Object>> dataList = new ArrayList<>();
		String cond = String.format("FNumber in (%s)","");
		List<VtuzxMap> materialQuery = vtuzxKingdeeService.query(client, "BD_STOCK", new VtuzxArrayList<>(
						"FName",
						"FNumber"
				),
				"", null, 0, 0);
		if (!VtuzxUtil.isEmpty(materialQuery)) {
			for (VtuzxMap data : materialQuery){
				Map<String,Object> map =new HashMap<>();
				String fname = VtuzxUtil.getString(data,"FName");
				String FNumber = VtuzxUtil.getString(data,"FNumber");
				map.put("value",FNumber);
				map.put("text",fname);
				dataList.add(map);
			}
		}
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}

	/**
	 *根据用户获取仓库
	 * @param param
	 * @param user_id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getStockListByUser(Map<String, Object> param, Long user_id) throws Exception {
		SysAdmin sysAdmin = adminService.getById(user_id);
		if (VtuzxUtil.isEmpty(sysAdmin.getStockNoList())){
			throw new RuntimeException("当前用户没有关联仓库,请维护");
		}
		String stockNoList = sysAdmin.getStockNoList();//仓库集合
		List<String> stockNos = Arrays.asList(stockNoList.split(","));
		String materialNoCond = org.springframework.util.StringUtils.collectionToDelimitedString(stockNos, ",", "'", "'");
		VtuzxKingdeeCloudClient client = null;
		try {
			client = vtuzxKingdeeService.loginKingdeeByUser(user_id);
		} catch (Exception e){
			throw new RuntimeException("金蝶账号密码错误，请维护");
		}
		List<Map<String,Object>> dataList = new ArrayList<>();
		String cond = String.format("FNumber in (%s)",materialNoCond);
		List<VtuzxMap> materialQuery = vtuzxKingdeeService.query(client, "BD_STOCK", new VtuzxArrayList<>(
						"FName",
						"FNumber"
				),
				cond, null, 0, 0);
		if (!VtuzxUtil.isEmpty(materialQuery)) {
			for (VtuzxMap data : materialQuery){
				Map<String,Object> map =new HashMap<>();
				String fname = VtuzxUtil.getString(data,"FName");
				String FNumber = VtuzxUtil.getString(data,"FNumber");
				map.put("stockNo",FNumber);
				map.put("stockName",fname);
				dataList.add(map);
			}
		}
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}

	/**
	 * 查询车间信息
	 * @param param
	 * @param user_id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getHosutList(Map<String, Object> param, Long user_id) throws Exception {
		VtuzxKingdeeCloudClient client = null;
		try {
			client = vtuzxKingdeeService.loginKingdeeByUser(user_id);
		} catch (Exception e){
			throw new RuntimeException("金蝶账号密码错误，请维护");
		}
		List<String> noList = new ArrayList<>();
		noList.add("BM000008");
		noList.add("BM000009");
		List<Map<String,Object>> dataList = new ArrayList<>();
		String materialNoCond = org.springframework.util.StringUtils.collectionToDelimitedString(noList, ",", "'", "'");
		String cond = String.format("FParentID.FNumber in (%s)",materialNoCond);
		List<VtuzxMap> materialQuery = vtuzxKingdeeService.query(client, "BD_Department", new VtuzxArrayList<>(
						"FName",
						"FNumber"
				),
				cond, null, 0, 0);
		if (!VtuzxUtil.isEmpty(materialQuery)) {
			for (VtuzxMap data : materialQuery){
				Map<String,Object> map =new HashMap<>();
				String fname = VtuzxUtil.getString(data,"FName");
				String FNumber = VtuzxUtil.getString(data,"FNumber");
				map.put("value",FNumber);
				map.put("text",fname);
				dataList.add(map);
			}
		}
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}


}